package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.DrawElementsFactory;
import com.ruoyi.common.DrawElementsTool;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.BmPaperContent;
import com.ruoyi.system.domain.Paper;
import com.ruoyi.system.domain.PaperType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BmPaper;
import com.ruoyi.system.service.IBmPaperService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【请填写功能名称】Controller
 * 
 * @author ruoyi
 * @date 2023-04-18
 */
@RestController
@RequestMapping("/system/paper")
public class BmPaperController extends BaseController
{
    @Autowired
    private IBmPaperService bmPaperService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询【请填写功能名称】列表
     */
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody BmPaper bmPaper)
    {
        String loginUserName = tokenService.getLoginUserName();
        bmPaper.setCreateBy(loginUserName);
        PageHelper.startPage(bmPaper.getPageNum(), bmPaper.getPageSize(),"paper_id desc").setReasonable(true);
        List<BmPaper> list = bmPaperService.selectBmPaperList(bmPaper);
        return getDataTable(list);
    }

    @PostMapping("/savePaper")
    public AjaxResult savePaper(@RequestBody Paper paper) throws IOException {
        AjaxResult ajaxResult = AjaxResult.success();
        String loginUserName = tokenService.getLoginUserName();
        BmPaper bmPaper = new BmPaper();
        bmPaper.setPaperTitle(paper.getTitle());
        bmPaper.setPaperType(1l);
        bmPaper.setCreateBy(loginUserName);
        bmPaper.setCreateTime(DateUtils.getNowDate());

        DrawElementsFactory drawElements = new DrawElementsFactory(paper).createDrawElements();
        //生成试卷
        paper.setExamType(PaperType.exam);
        String fileName = loginUserName + "_" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + ".pdf";
        String url = serverConfig.getUrl() + FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath(),fileName);
        url = url.replace("http","https");
        bmPaper.setPaperQuestionsUrl(url);
        new DrawElementsTool(drawElements).doDrawElements().SavePdfFile(fileName);
        //生成答案
        paper.setExamType(PaperType.answer);
        drawElements = new DrawElementsFactory(paper).createDrawElements();
        fileName = loginUserName + "_" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + ".pdf";
        url = serverConfig.getUrl() + FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath(),fileName);
        url = url.replace("http","https");
        bmPaper.setPaperAnswerUrl(url);
        new DrawElementsTool(drawElements).doDrawElements().SavePdfFile(fileName);
        //生成试卷和答案
        paper.setExamType(PaperType.examAndAnswer);
        drawElements = new DrawElementsFactory(paper).createDrawElements();
        fileName = loginUserName + "_" + new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + ".pdf";
        url = serverConfig.getUrl() + FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath(),fileName);
        url = url.replace("http","https");
        bmPaper.setPaperQuestionsanswerUrl(url);
        new DrawElementsTool(drawElements).doDrawElements().SavePdfFile(fileName);

        List<BmPaperContent> bmPaperContentList = new ArrayList<>();
        BmPaperContent bmPaperContent;
        for (int i = 0; i < paper.getWordList().size(); i++) {
            bmPaperContent = new BmPaperContent();
            List<String> strings = paper.getWordList().get(i);
            String join = String.join("", strings);
            bmPaperContent.setPaperWord(join);
            strings = paper.getPinyinList().get(i);
            join = String.join(";", strings);
            bmPaperContent.setPaperPinyin(join);
            bmPaperContent.setIsInWrongbook(0l);
            bmPaperContentList.add(bmPaperContent);

        }
        bmPaper.setPaperContentList(bmPaperContentList);
        BmPaper savePaper = bmPaperService.savePaper(bmPaper);
        ajaxResult.put("data",savePaper);
        return ajaxResult;
    }

    /**
     * 导出【请填写功能名称】列表
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BmPaper bmPaper)
    {
        List<BmPaper> list = bmPaperService.selectBmPaperList(bmPaper);
        ExcelUtil<BmPaper> util = new ExcelUtil<BmPaper>(BmPaper.class);
        util.exportExcel(response, list, "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */
    @GetMapping(value = "/{paperId}")
    public AjaxResult getInfo(@PathVariable("paperId") Long paperId)
    {
        return success(bmPaperService.selectBmPaperByPaperId(paperId));
    }

    /**
     * 新增【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BmPaper bmPaper)
    {
        return toAjax(bmPaperService.insertBmPaper(bmPaper));
    }

    /**
     * 修改【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BmPaper bmPaper)
    {
        return toAjax(bmPaperService.updateBmPaper(bmPaper));
    }

    /**
     * 删除【请填写功能名称】
     */
    @Log(title = "【请填写功能名称】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paperIds}")
    public AjaxResult remove(@PathVariable Long[] paperIds)
    {
        return toAjax(bmPaperService.deleteBmPaperByPaperIds(paperIds));
    }
}
